package edu.gatech.cs2340.thecyclonesquad;

import edu.gatech.cs2340.thecyclonesquad.database.Database;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

public class SearchCategoryActivity extends Activity implements OnItemSelectedListener {

	private ArrayAdapter<String> mListData11;
	Spinner spinner11;
	String[] categories = { "Category: Keepsakes", "Category: Heirlooms" , "Category: Misc" };
	String category;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_category);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchCategoryActivity.this, android.R.layout.simple_spinner_item, categories);
		spinner11 = (Spinner) findViewById(R.id.spin11);
		spinner11.setAdapter(adapter);
		spinner11.setOnItemSelectedListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_search_category, menu);
		return true;
	}
	
	/**
	 * Assigns values to category depending on spinner selection.
	 */
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		//gets position of one selected
		int position = spinner11.getSelectedItemPosition();
		switch(position) {
			case 0:
				category = "Keepsakes"; 
				break;
			case 1:
				category = "Heirlooms";
				break;
			case 2:
				category = "Misc";
				break;
		}
		
	}

	/**
	 * Method for if nothing selected in spinner.
	 */
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void searchByCategory(View view) {
		ListView lvList11 = (ListView)findViewById(R.id.listView11);
		Database entry = new Database(this);
    	entry.open();
    	String items[] = entry.searchCategory(category);
    	entry.close();
        mListData11 = new ArrayAdapter<String>(SearchCategoryActivity.this, android.R.layout.simple_list_item_1, items);
        //Set adapter to the list
        lvList11.setAdapter(mListData11);
		
	}

	/**
	 * logOut method logs the user out and goes back to login page.
	 * @param view is the basic building block for UI components
	 */
	public void logOut(View view) {
		Intent intent = new Intent(this, SignInActivity.class);
		startActivity(intent);			
	}

}
